##JOP Replication 
#Descriptive Figures -- Appendix

#packages
library(ggplot2)
library(extrafont)
library(dplyr)

#load data


#get unique group level obs
unique <- data %>% 
  distinct(dyad_id, .keep_all = TRUE)

#####Figure 1 Appendix#####

#Timing of Women's Joining


table(unique$womenyear_join)
num <- rbind(1,2,3,4,5,6, 7, 8, 9, 10)
obs <- rbind(29, 3, 4, 1, 1, 0, 0, 0, 0, 3)
years <- as.data.frame(cbind(num, obs))

colnames(years) <- c("First Year Women Included", "Number of Rebel Groups")
years$`Number of Rebel Groups` <- as.numeric(years$`Number of Rebel Groups`)

pdf("Plot_Timing_Women's Membership.pdf")
ggplot() + 
  geom_col(data = years,aes(y=years$`Number of Rebel Groups`, x=years$`First Year Women Included`), fill="black", width=0.7) +
  scale_x_continuous(breaks=(1:10)) +
  theme_bw()  +
  theme(axis.text = element_text(size=13), text = element_text(family = "Times New Roman"), axis.title=element_text(size=14), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), aspect.ratio=1.25) +
  ylab('Number of Rebel Groups') + 
  xlab("First Year of Women's Membership")  + 
  ggtitle("Timing of Women's Membership in Sample Observations")
dev.off()
embed_fonts("Plot_Timing_Women's Membership.pdf")


#####Figure 2 Appendix#####

#Coding Sources for All Observations
table(data$coding_type)
type <- rbind("First Mention", "Historical Account", "Both")
obs <- rbind(75, 31, 115)
codingobs <- as.data.frame(cbind(type, obs))

colnames(codingobs) <- c("Coding Source", "Number of Observations")
codingobs$`Number of Observations` <- as.numeric(codingobs$`Number of Observations`)

pdf("Plot_CodingSource_Obs.pdf")
ggplot() + 
  geom_col(data = codingobs,aes(y=`Number of Observations`, x=`Coding Source`), fill="black", width=0.7) +
  theme_bw()  +
  theme(axis.text = element_text(size=13), text = element_text(family = "Times New Roman"), axis.title=element_text(size=14), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), aspect.ratio=1.25) +
  ylab('Number of Observations') + 
  xlab("Coding Source")  + 
  ggtitle("Coding Sources for Observations in Dataset")
dev.off()
embed_fonts("Plot_CodingSource_Obs.pdf")

######Figure 3 Appendix#####

##Coding Source by Group - highest level
table(unique$coding_highest)
type <- rbind("First Mention", "First Mention & Historical Account")
obs <- rbind(18, 20)
codingobs <- as.data.frame(cbind(type, obs))

colnames(codingobs) <- c("Coding Source", "Number of Observations")
codingobs$`Number of Observations` <- as.numeric(codingobs$`Number of Observations`)

pdf("Plot_CodingSource_Group.pdf")
ggplot() + 
  geom_col(data = codingobs,aes(y=`Number of Observations`, x=`Coding Source`), fill="black", width=0.7) +
  theme_bw()  +
  theme(axis.text = element_text(size=13), axis.title=element_text(size=14), text = element_text(family = "Times New Roman"), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), aspect.ratio=1.25) +
  ylab('Number of Groups') + 
  xlab("Coding Source")  + 
  ggtitle("Highest Coding Source for Groups")
dev.off()
embed_fonts("Plot_CodingSource_Group.pdf")


#####Figure 4 Appendix#####

#Coding Groups w/o Women
table(unique$coding_type_zero)
type <- rbind("No Evidence", "Confirmation of No Women")
obs <- rbind(46, 20)
codingobs <- as.data.frame(cbind(type, obs))

colnames(codingobs) <- c("Coding Source", "Number of Observations")
codingobs$`Number of Observations` <- as.numeric(codingobs$`Number of Observations`)

pdf("Plot_CodingSource_Zero.pdf")
ggplot() + 
  geom_col(data = codingobs,aes(y=`Number of Observations`, x=`Coding Source`), fill="black", width=0.7) +
  theme_bw()  +
  theme(axis.text = element_text(size=13), axis.title=element_text(size=14), text = element_text(family = "Times New Roman"), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), aspect.ratio=1.25) +
  ylab('Number of Groups') + 
  xlab("Coding Source")  + 
  ggtitle("Coding Source for Observations of No Women")
dev.off()
embed_fonts("Plot_CodingSource_Zero.pdf")


